package cn.pconline.autoclub.service.bbssync;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubIntegral;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.DynaService;
import cn.pconline.autoclub.service.PetrolService;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2014年12月4日 上午11:18:44
 * {"agent":0,"authorId":72402,"createAt":1418385282000,"createIp":"192.168.32.66","key":"autobbs.topic.cancelpick5805768","routeKey":"autobbs.topic.cancelpick","tid":5805768}
 */
public class SyncCancelPickTopicService extends BbsSyncPetrolService {
	@Autowired
	private DynaService dynaService;
	@Autowired
	private PetrolService petrolService;
    
	@Override
    protected int addPetroltoMember(ClubMember clubMember, JSONObject msg) {
	    ClubIntegral clubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_PICK_CANCEL);
	    String reason = clubIntegral.getName() + "(topic-" + getTopicId(msg) + ")";
        petrolService.addOnlyPersonWithinLimit(clubMember, (int) clubIntegral.getIntegralId(), clubIntegral.getPetrol(), reason, false);
        return clubIntegral.getPetrol();
    }

    @Override
    protected int addPetroltoClub(Club club, JSONObject msg) {
        ClubIntegral clubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_PICK_CANCEL);
        String reason = clubIntegral.getName() + "(topic-" + getTopicId(msg) + ")";
        petrolService.addToClubWithinLimit(club.getClubId(), (int) clubIntegral.getIntegralId(), clubIntegral.getPetrol(), reason, false);
        return clubIntegral.getPetrol();
    }
    
    @Override
    protected long getUserId(JSONObject msg) {
        return msg.getLongValue("authorId");
    }
	
	@Override
	public void process(JSONObject msg, ClubMember clubMember) {
		//验证用户
	    long userId = getUserId(msg);
		if (userId <= 0L) {
            log.info("not userId-{}", userId);
            return;
        }

        //判断用户是否登录
        User user = userService.authUser(userId);
        if (user == null || user.getUid() == 0L) {
            log.info("not found user-{}", userId);
            return;
        }

        int petrol = this.calculateDynaPetrol();
        log.info("cancel pick add petrol: {}", petrol);
        //取消精华
        long topicId = getTopicId(msg);
        Date createAt = getTopicCreateAt(msg);
        dynaService.removePick(topicId, createAt, petrol);
	}

    /**
     * 计算取消精华油值
     * @param topicDyna
     * @return
     */
    public int calculateDynaPetrol() {
        ClubIntegral clubIntegral = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_PICK_CANCEL);
        int dynaPetrol = clubIntegral.getPetrol();
        return dynaPetrol;
    }
    
    private long getTopicId(JSONObject msg) {
        return msg.getLongValue("topicId");
    }
    
    private Date getTopicCreateAt(JSONObject msg) {
        return msg.getDate("createAt");
    }
}
